// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcMatrix3.h"
#include "MgcSpotLight.h"

MgcImplementRTTI(MgcSpotLight,MgcPointLight);
MgcImplementStream(MgcSpotLight);

//---------------------------------------------------------------------------
MgcSpotLight::MgcSpotLight ()
    :
    m_kDirection(-MgcVector3::UNIT_Z)
{
    SetAngle(MgcMath::PI);
    m_fExponent = 0.0;
}
//---------------------------------------------------------------------------
void MgcSpotLight::SetAngle (MgcReal fAngle)
{
    m_fAngle = fAngle;
    m_fCosSqr = MgcMath::Cos(m_fAngle);
    m_fCosSqr *= m_fCosSqr;
    m_fSinSqr = MgcMath::Sin(m_fAngle);
    m_fSinSqr *= m_fSinSqr;
}
//---------------------------------------------------------------------------
void MgcSpotLight::ComputeDiffuse (const MgcMatrix3& rkWorldRotate,
    const MgcVector3& rkWorldTranslate, MgcReal fWorldScale,
    const MgcVector3* akVertex, const MgcVector3* akNormal,
    unsigned int uiQuantity, const bool* abVisible, MgcColor* akDiffuse)
{
    // transform light position to model space of old mesh
    MgcReal fInvWScale = 1.0/fWorldScale;
    MgcVector3 kDiff = m_kLocation - rkWorldTranslate;
    MgcVector3 kModelPos = (kDiff*rkWorldRotate)*fInvWScale;
    MgcVector3 kModelDir = m_kDirection*rkWorldRotate;

    // adjust diffuse color by light intensity
    MgcColor kAdjDiffuse = m_fIntensity*m_kDiffuse;

    for (unsigned int uiV = 0; uiV < uiQuantity; uiV++)
    {
        if ( abVisible[uiV] )
        {
            kDiff = kModelPos - akVertex[uiV];
            MgcReal fDiffDotDir = kDiff.Dot(kModelDir);
            if ( fDiffDotDir >= 0.0 )
                continue;

            MgcReal fLenSqr = kDiff.Dot(kDiff);
            MgcReal fDiffDotDirSqr = fDiffDotDir*fDiffDotDir;
            MgcReal fAngleAttenuate = fDiffDotDirSqr - fLenSqr*m_fCosSqr;
            if ( fAngleAttenuate <= 0.0 )
                continue;

            MgcReal fDot = kDiff.Dot(akNormal[uiV]);
            if ( fDot > 0.0 )
            {
                MgcReal fNumer = fDot*fAngleAttenuate;
                MgcReal fDenom =
                    fDiffDotDirSqr*m_fSinSqr*MgcMath::Sqrt(fLenSqr);
                akDiffuse[uiV] += (fNumer/fDenom)*kAdjDiffuse;
            }
        }
    }
}
//---------------------------------------------------------------------------
void MgcSpotLight::ComputeSpecular (const MgcMatrix3& rkWorldRotate,
    const MgcVector3& rkWorldTranslate, MgcReal fWorldScale,
    const MgcVector3* akVertex, const MgcVector3* akNormal,
    unsigned int uiQuantity, const bool* abVisible,
    const MgcVector3& rkCameraModelLocation, MgcColor* akSpecular)
{
    // transform light position to model space of old mesh
    MgcReal fInvWScale = 1.0/fWorldScale;
    MgcVector3 kDiff = m_kLocation - rkWorldTranslate;
    MgcVector3 kModelPos = (kDiff*rkWorldRotate)*fInvWScale;
    MgcVector3 kModelDir = m_kDirection*rkWorldRotate;

    // adjust diffuse color by light intensity
    MgcColor kAdjSpecular = m_fIntensity*m_kSpecular;

    for (unsigned int uiV = 0; uiV < uiQuantity; uiV++)
    {
        if ( abVisible[uiV] )
        {
            kDiff = kModelPos - akVertex[uiV];
            MgcReal fDiffDotDir = kDiff.Dot(kModelDir);
            if ( fDiffDotDir >= 0.0 )
                continue;

            MgcReal fLenSqr = kDiff.Dot(kDiff);
            MgcReal fDiffDotDirSqr = fDiffDotDir*fDiffDotDir;
            MgcReal fAngleAttenuate = fDiffDotDirSqr - fLenSqr*m_fCosSqr;
            if ( fAngleAttenuate <= 0.0 )
                continue;

            MgcReal fDot = kDiff.Dot(akNormal[uiV]);
            MgcVector3 kReflect = (2.0*fDot)*akNormal[uiV] - kDiff;
            MgcVector3 kViewDir = rkCameraModelLocation - akVertex[uiV];
            fDot = kViewDir.Dot(kReflect);
            if ( fDot > 0.0 )
            {
                MgcReal fNumer = fDot*fDot*fAngleAttenuate;
                MgcReal fDenom = fDiffDotDirSqr*m_fSinSqr *
                    MgcMath::Sqrt(fLenSqr)*kViewDir.Dot(kViewDir);
                akSpecular[uiV] += (fNumer/fDenom)*kAdjSpecular;
            }
        }
    }
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcSpotLight::Factory (MgcStream& rkStream)
{
    MgcSpotLight* pkObject = new MgcSpotLight;
    MgcStream::Link* pkLink = new MgcStream::Link(pkObject);
    pkObject->Load(rkStream,pkLink);
    return pkObject;
}
//---------------------------------------------------------------------------
void MgcSpotLight::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcPointLight::Load(rkStream,pkLink);

    // native data
    MgcStreamRead(rkStream,m_kDirection);
    MgcStreamRead(rkStream,m_fAngle);
    MgcStreamRead(rkStream,m_fExponent);
}
//---------------------------------------------------------------------------
void MgcSpotLight::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcPointLight::Link(rkStream,pkLink);
}
//---------------------------------------------------------------------------
bool MgcSpotLight::Register (MgcStream& rkStream)
{
    return MgcPointLight::Register(rkStream);
}
//---------------------------------------------------------------------------
void MgcSpotLight::Save (MgcStream& rkStream)
{
    MgcPointLight::Save(rkStream);

    // native data
    MgcStreamWrite(rkStream,m_kDirection);
    MgcStreamWrite(rkStream,m_fAngle);
    MgcStreamWrite(rkStream,m_fExponent);
}
//---------------------------------------------------------------------------
